package com.holly.unit.deform.api.util;

import com.holly.unit.deform.api.factory.ConvertFactory;
import com.holly.unit.deform.api.factory.FieldCommentConverter;
import com.holly.unit.deform.api.pojo.tool.DesformWidget;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 类描述:
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/14 14:38
 */
public class ConvertUtil {

    /**
     * text转value
     */
    public static final int in = 2;
    /**
     * value转text
     */
    public static final int out = 1;

    /**
     * 用户 表名
     */
    public static final String USER_TABLE_NAME = "SYS_USER";
    /**
     * 用户名称字段名
     */
    public static final String USER_REAL_NAME = "REALNAME";
    /**
     * 用户账号字段名
     */
    public static final String USER_LOGIN_NAME = "USERNAME";
    /**
     * ID字段名
     */
    public static final String ID_NAME = "ID";

    /**
     * 部门 表名
     */
    public static final String DEPART_TABLE_NAME = "SYS_DEPART";
    /**
     * 部门名称字段名
     */
    public static final String DEPART_TEXT_NAME = "DEPART_NAME";
    /**
     * 部门 orgCode
     */
    public static final String DEPART_ORG_CODE_NAME = "orgCode";

    /**
     * 转换
     *
     * @param flag       1value转text 2text转value
     * @param dataList
     * @param widgetList
     */
    public static void doConvertDataList(int flag, List<Map<String, Object>> dataList, List<DesformWidget> widgetList) {
        Map<String, FieldCommentConverter> converterMap = ConvertFactory.getAllConverterByFieldConfig(widgetList);
        for (Map<String, Object> map : dataList) {
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
            //map循环中无法新增元素 特加此otherValue作为新增元素存储的容器
            Map<String, Object> otherValue = new HashMap<>();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                Object temp = entry.getValue();
                if (temp == null) {
                    continue;
                }
                String key = entry.getKey();
                FieldCommentConverter converter = converterMap.get(key);
                if (converter != null) {
                    String tempString = temp.toString();
                    Object real = flag == out ? converter.converterToTxt(tempString) : converter.converterToVal(tempString);
                    //System.out.println("原来的值="+tempString);
                    //System.out.println("替换的值="+real);
                    linkDownConfigHandler(converter, map, flag);
                    treeCategoryConfigHandler(converter, otherValue, tempString);
                    map.put(key, real);
                }
            }
            for (String key : otherValue.keySet()) {
                map.put(key, otherValue.get(key));
            }
        }
    }

    /**
     * 级联下拉额外字段处理逻辑
     *
     * @param converter
     * @param data
     * @param flag
     */
    private static void linkDownConfigHandler(FieldCommentConverter converter, Map<String, Object> data, int flag) {
//        Map<String, String> special = converter.getConfig();
//        if (special != null) {
//            String linkField = special.get("linkField");
//            if (oConvertUtils.isNotEmpty(linkField)) {
//                for (String lf : linkField.split(",")) {
//                    Object temp = data.get(lf);
//                    if (temp == null) {
//                        continue;
//                    }
//                    String tempString = temp.toString();
//                    String real = flag == 1 ? converter.converterToTxt(tempString) : converter.converterToVal(tempString);
//                    data.put(lf, real);
//                }
//            }
//        }
    }

    /**
     * 分类字典额外字段处理逻辑
     *
     * @param converter
     * @param otherValue
     * @param currValue
     */
    private static void treeCategoryConfigHandler(FieldCommentConverter converter, Map<String, Object> otherValue, String currValue) {
//        Map<String, String> special = converter.getConfig();
//        if (special != null) {
//            //分类字典树额外字段处理逻辑
//            String treeText = special.get("treeText");
//            if (oConvertUtils.isNotEmpty(treeText)) {
//                otherValue.put(treeText, currValue);
//            }
//        }
    }

}
